home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 41 / Amiga Format CD41 (1999-06)(Future Publishing)(GB)[!][issue 1999-07].iso / -seriously_amiga- / programming / other / gtlayout / source / lt_initexit.c < prev    next >
C/C++ Source or Header  |  1999-04-19  |  6KB  |  332 lines

  1. /*
  2. **    GadTools layout toolkit
  3. **
  4. **    Copyright © 1993-1998 by Olaf `Olsen' Barthel
  5. **        Freely distributable.
  6. **
  7. **    :ts=4
  8. */
  9.  
  10. #ifndef _GTLAYOUT_GLOBAL_H
  11. #include "gtlayout_global.h"
  12. #endif
  13.  
  14. /*****************************************************************************/
  15.  
  16. #define NO !
  17.  
  18. /*****************************************************************************/
  19.  
  20. #include <exec/memory.h>
  21. #include <libraries/locale.h>
  22.  
  23. #include <clib/alib_protos.h>    /* For NewList */
  24.  
  25. #include <clib/locale_protos.h>
  26. #include <pragmas/locale_pragmas.h>
  27.  
  28. /*****************************************************************************/
  29.  
  30. #include "Assert.h"
  31.  
  32. /*****************************************************************************/
  33.  
  34. STATIC struct LocaleBase *    LocaleBase;
  35. STATIC struct Locale *        DefaultLocale;
  36.  
  37. /*****************************************************************************/
  38.  
  39.  
  40. /****** gtlayout.library/LT_Init ******************************************
  41. *
  42. *   NAME
  43. *    LT_Init -- Initialize user interface code.
  44. *
  45. *   SYNOPSIS
  46. *    LT_Init();
  47. *
  48. *    VOID LT_Init(VOID);
  49. *
  50. *   FUNCTION
  51. *    You need to initialize the user interface code only once,
  52. *    so it can set up its internals, open libraries, etc.
  53. *    The code has to be initialized before any user interface
  54. *    creation can take place.
  55. *
  56. *   NOTES
  57. *    This function is not present in the shared library, only
  58. *    in the link library. You need not and cannot invoke it in
  59. *    the shared library.
  60. *
  61. *   SEE ALSO
  62. *    gtlayout.library/LT_Exit
  63. *
  64. ******************************************************************************
  65. *
  66. */
  67.  
  68. /*****************************************************************************/
  69.  
  70. struct LibraryInitEntry
  71. {
  72.     BOOL                MustOpen;
  73.     STRPTR                Name;
  74.     struct Library **    Library;
  75. };
  76.  
  77. struct ClassInitEntry
  78. {
  79.     STRPTR                ClassName;
  80.     LONG                InstanceSize;
  81.     HOOKFUNC            Dispatcher;
  82.     Class **            ClassPtr;
  83. };
  84.  
  85. /*****************************************************************************/
  86.  
  87. BOOL LIBENT
  88. LT_Init(VOID)
  89. {
  90.     BOOL success = TRUE;
  91.  
  92.     #ifndef LINK_LIB
  93.     {
  94.         extern struct Library __far * AbsExecBase;
  95.  
  96.         SysBase = AbsExecBase;
  97.     }
  98.     #endif    // LINK_LIB
  99.  
  100.     if(SysBase == NULL || SysBase->lib_Version < 37)
  101.     {
  102.         success = FALSE;
  103.     }
  104.     else
  105.     {
  106.         STATIC struct LibraryInitEntry InitTable[] =
  107.         {
  108.             TRUE,    "intuition.library",    &IntuitionBase,
  109.             TRUE,    "graphics.library",        &GfxBase,
  110.             TRUE,    "utility.library",        &UtilityBase,
  111.             TRUE,    "gadtools.library",        &GadToolsBase,
  112.             TRUE,    "keymap.library",        &KeymapBase,
  113.             FALSE,    "locale.library",        (struct Library **)&LocaleBase
  114.         };
  115.  
  116.         STATIC struct ClassInitEntry ClassInitTable[] =
  117.         {
  118.             IMAGECLASS,    
  119.             sizeof(ImageInfo),
  120.             (HOOKFUNC)LTP_ImageDispatch,
  121.             <P_ImageClass,
  122.  
  123.             #ifdef DO_LEVEL_KIND
  124.             {
  125.                 GADGETCLASS,
  126.                 sizeof(struct SliderClassData),
  127.                 (HOOKFUNC)LTP_LevelClassDispatcher,
  128.                 <P_LevelClass,
  129.             },
  130.             #endif    /* DO_LEVEL_KIND */
  131.  
  132.             #ifdef DO_POPUP_KIND
  133.             {
  134.                 GADGETCLASS,
  135.                 sizeof(PopInfo),
  136.                 (HOOKFUNC)LTP_PopupClassDispatcher,
  137.                 <P_PopupClass,
  138.             },
  139.             #endif    /* DO_POPUP_KIND */
  140.  
  141.             #ifdef DO_TAB_KIND
  142.             {
  143.                 GADGETCLASS,
  144.                 sizeof(TabInfo),
  145.                 (HOOKFUNC)LTP_TabClassDispatcher,
  146.                 <P_TabClass,
  147.             },
  148.             #endif    /* DO_TAB_KIND */
  149.         };
  150.  
  151.         LONG i;
  152.  
  153.         V39 = (BOOLEAN)(SysBase->lib_Version >= 39);
  154.         V40 = (BOOLEAN)(SysBase->lib_Version >= 40);
  155.  
  156.         InitSemaphore(<P_LockSemaphore);
  157.         NewList((struct List *)<P_LockList);
  158.  
  159.         NewList((struct List *)<P_EmptyList);
  160.  
  161.         #ifdef DO_PICKSHORTCUTS
  162.         {
  163.             InitSemaphore(<P_KeySemaphore);
  164.     
  165.             if(!(LTP_Keys[0] = (UBYTE *)AllocMem(512,MEMF_ANY|MEMF_CLEAR)))
  166.             {
  167.                 success = FALSE;
  168.             }
  169.         }
  170.         #endif    /* DO_PICKSHORTCUTS */
  171.  
  172.         for(i = 0 ; i < NUMELEMENTS(InitTable) ; i++)
  173.         {
  174.             (*InitTable[i].Library) = OpenLibrary(InitTable[i].Name,37);
  175.             if((*InitTable[i].Library) == NULL && InitTable[i].MustOpen)
  176.             {
  177.                 success = FALSE;
  178.             }
  179.         }
  180.  
  181.         if(success)
  182.         {
  183.             LTP_NumberFormat = "%ld";
  184.             LTP_DecimalPoint = ".";
  185.  
  186.             if(LocaleBase != NULL)
  187.             {
  188.                 /* Guaranteed to open. */
  189.                 DefaultLocale = OpenLocale(NULL);
  190.  
  191.                 LTP_DecimalPoint = DefaultLocale->loc_DecimalPoint;
  192.  
  193.                 if(LocaleBase->lb_SysPatches)
  194.                     LTP_NumberFormat = "%lD";
  195.             }
  196.  
  197.             for(i = 0 ; i < NUMELEMENTS(ClassInitTable) ; i++)
  198.             {
  199.                 (*ClassInitTable[i].ClassPtr) = MakeClass(NULL,ClassInitTable[i].ClassName,NULL,ClassInitTable[i].InstanceSize,0);
  200.                 if((*ClassInitTable[i].ClassPtr) != NULL)
  201.                 {
  202.                     (*ClassInitTable[i].ClassPtr)->cl_Dispatcher.h_Entry = ClassInitTable[i].Dispatcher;
  203.                 }
  204.                 else
  205.                 {
  206.                     success = FALSE;
  207.                     break;
  208.                 }
  209.             }
  210.         }
  211.     }
  212.  
  213.     if(NO success)
  214.     {
  215.         LT_Exit();
  216.     }
  217.  
  218.     return(success);
  219. }
  220.  
  221.  
  222. /*****************************************************************************/
  223.  
  224.  
  225. /****** gtlayout.library/LT_Exit ******************************************
  226. *
  227. *   NAME
  228. *    LT_Exit -- Clean up user interface allocations
  229. *
  230. *   SYNOPSIS
  231. *    LT_Exit();
  232. *
  233. *    VOID LT_Exit(VOID);
  234. *
  235. *   FUNCTION
  236. *    When you are finished with user interface creation and
  237. *    do not not need the code any more you should call this
  238. *    routine. It will free all the memory allocated by
  239. *    LT_Init(), close libraries, etc.
  240. *
  241. *   INPUTS
  242. *    none
  243. *
  244. *   RESULT
  245. *    none
  246. *
  247. *   NOTES
  248. *    This function is not present in the shared library, only
  249. *    in the link library. You need not and cannot invoke it in
  250. *    the shared library.
  251. *
  252. *   SEE ALSO
  253. *    gtlayout.library/LT_Init
  254. *
  255. ******************************************************************************
  256. *
  257. */
  258.  
  259. VOID LIBENT
  260. LT_Exit(VOID)
  261. {
  262.     if(SysBase != NULL && SysBase->lib_Version >= 37)
  263.     {
  264.         #ifdef DO_PICKSHORTCUTS
  265.         {
  266.             FreeMem(LTP_Keys[0],512);
  267.             LTP_Keys[0] = LTP_Keys[1] = NULL;
  268.         }
  269.         #endif
  270.  
  271.         #ifdef DO_LEVEL_KIND
  272.         {
  273.             if(LTP_LevelClass)
  274.             {
  275.                 FreeClass(LTP_LevelClass);
  276.                 LTP_LevelClass = NULL;
  277.             }
  278.         }
  279.         #endif    /* DO_LEVEL_KIND */
  280.  
  281.         #ifdef DO_POPUP_KIND
  282.         {
  283.             if(LTP_PopupClass)
  284.             {
  285.                 FreeClass(LTP_PopupClass);
  286.                 LTP_PopupClass = NULL;
  287.             }
  288.         }
  289.         #endif    // DO_POPUP_KIND
  290.  
  291.         #ifdef DO_TAB_KIND
  292.         {
  293.             if(LTP_TabClass)
  294.             {
  295.                 FreeClass(LTP_TabClass);
  296.                 LTP_TabClass = NULL;
  297.             }
  298.         }
  299.         #endif    // DO_TAB_KIND
  300.  
  301.         if(LTP_ImageClass)
  302.         {
  303.             FreeClass(LTP_ImageClass);
  304.             LTP_ImageClass = NULL;
  305.         }
  306.  
  307.         if(DefaultLocale)
  308.         {
  309.             CloseLocale(DefaultLocale);
  310.             DefaultLocale = NULL;
  311.         }
  312.  
  313.         CloseLibrary(LocaleBase);
  314.         LocaleBase = NULL;
  315.  
  316.         CloseLibrary(KeymapBase);
  317.         KeymapBase = NULL;
  318.  
  319.         CloseLibrary(GadToolsBase);
  320.         GadToolsBase = NULL;
  321.  
  322.         CloseLibrary(UtilityBase);
  323.         UtilityBase = NULL;
  324.  
  325.         CloseLibrary(GfxBase);
  326.         GfxBase = NULL;
  327.  
  328.         CloseLibrary(IntuitionBase);
  329.         IntuitionBase = NULL;
  330.     }
  331. }
  332.